package taskmaster.job;

import javax.swing.*;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.*;


/**
 * Created by IntelliJ IDEA.
 * User: abhatnag
 * Date: 7/27/11
 * Time: 9:59 AM
 * To create complete job graphs with all the tasks in it.It does not take the task statuses
 * in account like the original Taskcollection class and can be used in the Edit mode
 */
public class JobAllTasksCollection
{
    String jobName;
    Connection con;
    String lib;
    public List<Dependency> dependency_task = new ArrayList<Dependency>();
    public LinkedHashMap<String, Character> task_type = new LinkedHashMap<String, Character>();
    public HashSet<Task> alljobTasks = new HashSet<Task>();
    public HashMap<String, String> defaultNames = new HashMap<String, String>();

    public JobAllTasksCollection(Connection con, String jobName, String lib)
    {

        this.jobName = jobName;
        this.con = con;
        this.lib = lib;
        produceJobstructure();
    }

    public void produceJobstructure()
    {
        try
        {
            Statement stmt = con.createStatement();
            ResultSet rs1 = stmt.executeQuery("select * from tms.tasks where type <> 'D' START WITH predecessor_tag IS NULL AND JOB_TAG='" + jobName + "'connect by PRIOR tasks.task_tag= tasks.PREDECESSOR_TAG AND tasks.JOB_TAG='" + jobName + "'order by level");

            while (rs1.next())
            {
                alljobTasks.add(new Task(rs1.getString("task_tag"), rs1.getString("job_tag"), rs1.getInt("role_id"), new Character(rs1.getString("type").charAt(0)), rs1.getString("description"), rs1.getString("command"), rs1.getTime("start_time"), rs1.getInt("Timeout"), rs1.getInt("Retries")));
                task_type.put(rs1.getString("task_tag"), new Character(rs1.getString("type").charAt(0)));
            }
            rs1.close();
            stmt.close();
        } catch (Exception e)
        {
            JOptionPane.showMessageDialog(null, "Error getting tasks from DB to create a graph: " + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
            //  e.printStackTrace();
        }

        try
        {
            Statement stmt = con.createStatement();
            ResultSet rs1 = stmt.executeQuery("select task_tag,dependency_tag,description from tms.task_dependencies where active=1 and job_tag='" + jobName + "'");


            while (rs1.next())
            {
                dependency_task.add(new Dependency(rs1.getString("dependency_tag"), rs1.getString("task_tag"), rs1.getString("description")));

            }
            rs1.close();
            stmt.close();

        } catch (Exception e)
        {
            JOptionPane.showMessageDialog(null, "Error getting dependencies from DB to create  a graph: " + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
        }

        defaultNames = getDefaultTaskDisplayName(alljobTasks);
    }

    private HashMap<String, String> getDefaultTaskDisplayName(HashSet<Task> alljobTasks)
    {
        SimpleDateFormat timeFormat = new SimpleDateFormat("h:mm a");
        Task[] jobTasks = new Task[alljobTasks.size()];
        jobTasks = alljobTasks.toArray(jobTasks);

        for (int i = 0; i < jobTasks.length; i++)
        {
            String displayName = jobTasks[i].getTaskTag().replace('_', ' ');
            if (jobTasks[i].getCronStartTime() != null)
            {
                Date d = new java.util.Date(jobTasks[i].getCronStartTime().getTime());
                if (lib.equalsIgnoreCase("JGraphX"))
                {
                    displayName += "\n" + "(" + timeFormat.format(d) + ") ";
                } else
                {
                    displayName += "\\n" + "(" + timeFormat.format(d) + ") ";
                }
            }
            defaultNames.put(jobTasks[i].getTaskTag(), displayName);
        }
        return defaultNames;
    }
}
